package com.cosina.test;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLUtils;

public class Lession6Render extends BaseRender{
	float xrot, yrot, zrot;
	int texture = -1;
	int one = 0x10000;
	 
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		super.onSurfaceCreated(gl, config);
		gl.glClearDepthf(1.0f);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		IntBuffer intBuffer = IntBuffer.allocate(1);
		gl.glGenTextures(1, intBuffer);
		texture = intBuffer.get();
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, DataManager.getBm(R.drawable.nehe), 0);
		
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
	}
	@Override
	public void onDrawFrame(GL10 gl) {
		super.baseDrawFrame(gl);
		

		IntBuffer vertices = IntBuffer.wrap(new int[]{
				-one,-one,one,
				one,-one,one,
				one,one,one,
				-one,one,one,
				
				-one,-one,-one,
				-one,one,-one,
				one,one,-one,
				one,-one,-one,
				
				-one,one,-one,
				-one,one,one,
				one,one,one,
				one,one,-one,
				
				-one,-one,-one,
				one,-one,-one,
				one,-one,one,
				-one,-one,one,
				
				one,-one,-one,
				one,one,-one,
				one,one,one,
				one,-one,one,
				
				-one,-one,-one,
				-one,-one,one,
				-one,one,one,
				-one,one,-one,
				
		});
		IntBuffer texCoords = IntBuffer.wrap(new int[]{
			one,0,0,0,0,one,one,one,	
			0,0,0,one,one,one,one,0,
			one,one,one,0,0,0,0,one,
			0,one,one,one,one,0,0,0,
			0,0,0,one,one,one,one,0,
			one,0,0,0,0,one,one,one,
		});
		
		ByteBuffer indices = ByteBuffer.wrap(new byte[]{
				0,1,3,2,
				4,5,7,6,
				8,9,11,10,
				12,13,15,14,
				16,17,19,18,
				20,21,23,22,
		});
		
		gl.glTranslatef(0.0f, 0.0f, -5.0f);
		
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices);
		gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords);
		
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	
//		 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0 , 24);
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24,  GL10.GL_UNSIGNED_BYTE, indices);
	    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	    
	    xrot+=0.3f; /* X Axis Rotation */
	    yrot+=0.2f; /* Y Axis Rotation */
	    zrot+=0.4f; /* Z Axis Rotation */
	}
}